package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.BuyRecord;
import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.BuyRecordQueryObject;
import cn.wolfcode.crm.qo.CustomerQueryObject;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IBuyRecordService;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.ICONST;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/buyRecord")
public class BuyRecordController {

    @Autowired
    private IBuyRecordService buyRecordService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    // 购买新增或修改
    @RequestMapping("/save")
    @ResponseBody
    public JsonResult saveOrUpdate(BuyRecord buyRecord){
        try{
                buyRecordService.save(buyRecord);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"购买新增失败");
        }
        return new JsonResult(true,"购买新增成功!!!"); // localhost/buyRecord/list.do
    }

    // 查询所有购买数据
    @RequestMapping("/buyRecordList")
    public String list(Model model, @ModelAttribute("qo")BuyRecordQueryObject qo){
        //查询所有的课程
       List<SystemDictionaryItem> courses=  systemDictionaryItemService.queryBySystemDictionaryCourse("course");
        model.addAttribute("courses",courses);
        List<Customer> customers = customerService.list();
        model.addAttribute("customers",customers);
        PageInfo query = buyRecordService.query(qo);
        model.addAttribute("pageResult",query);
        return "buyRecord/buyRecordList";
    }
}
